package com.sys.app.web.controller;



import com.sys.app.entity.util.SecrecyUtil;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;

import java.io.IOException;

@Controller
@RequestMapping("demo")
public class DemoContorller {
    /**
     * 图标库
     * @return
     */
    @GetMapping("/icon.do")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("main/demo/icon");
        return modelAndView;
    }
    /**
     * input
     * @return
     */
    @GetMapping("/input.do")
    public ModelAndView input(){
        ModelAndView modelAndView = new ModelAndView("main/demo/input");
        return modelAndView;
    }
    /**
     * form表单
     * @return
     */
    @GetMapping("/form.do")
    public ModelAndView form(){
        ModelAndView modelAndView = new ModelAndView("main/demo/form");
        return modelAndView;
    }

    @PostMapping("/getRequestBody.do")
    public void getRequestBody(HttpServletRequest request){
        String bodyparam = getRequestBody1(request);
        System.out.println("first--param-->" + bodyparam);

        bodyparam = SecrecyUtil.encryptAES(bodyparam, SecrecyUtil.getAESKey("123456"));
        System.out.println("json--param-->" + bodyparam);

        bodyparam = SecrecyUtil.decryptAES(bodyparam, SecrecyUtil.getAESKey("123456"));

        System.out.println("json--param-->" + bodyparam);
    }
    private static String getRequestBody1(HttpServletRequest request) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        String str = "";
        try {
            br = request.getReader();
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
            if (br != null) {
                try {
                    br.close();
                } catch (IOException eo) {
                    eo.printStackTrace();
                }
            }
            if (br != null)
                try {
                    br.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
        }
        finally
        {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
}
